﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SystemDirectory.Biz;

namespace SystemDirectory
{
    public partial class AddressLookups : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                AddressBO bo = new AddressBO();
                DataTable dtAddress = bo.Get(postcode.Text, street.Text, town.Text);

                PagedDataSource pds = new PagedDataSource();
                DataView dv = new DataView(dtAddress);
                pds.DataSource = dv;
                pds.AllowPaging = true;
                pds.PageSize = 2;
                int NumPage = pds.PageCount;

                int CurrentPage;
                if (Request.QueryString["page"] != null)
                {
                    CurrentPage = Convert.ToInt32(Request.QueryString["page"].ToString());
                }
                else
                {
                    CurrentPage = 1;
                }

                pds.CurrentPageIndex = CurrentPage - 1;

                if (!pds.IsFirstPage)
                {
                    hplBackward.Visible = true;
                    hplBackward.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurrentPage - 1);
                }
                else
                {
                    hplBackward.Visible = false;
                }
                if (!pds.IsLastPage)
                {
                    hplForward.Visible = true;
                    hplForward.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurrentPage + 1);
                }
                else
                {
                    hplForward.Visible = false;
                }


                rptAddress.DataSource = pds;
                rptAddress.DataBind();
                lblCurrent.Text = CurrentPage.ToString();
                lblTotal.Text = NumPage.ToString();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AddressBO addressBo = new AddressBO();
            DataTable dtContact = addressBo.Get(postcode.Text, street.Text, town.Text);
            PagedDataSource pds = new PagedDataSource();
            DataView dv = new DataView(dtContact);
            pds.DataSource = dv;
            pds.AllowPaging = true;
            pds.PageSize = 2;
            int NumPage = pds.PageCount;

            int CurrentPage;
            if (Request.QueryString["page"] != null)
            {
                CurrentPage = Convert.ToInt32(Request.QueryString["page"].ToString());
            }
            else
            {
                CurrentPage = 1;
            }

            pds.CurrentPageIndex = CurrentPage - 1;

            if (!pds.IsFirstPage)
                hplBackward.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurrentPage - 1);
            else
            {

                hplBackward.Visible = false;
            }
            if (!pds.IsLastPage)
                hplForward.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurrentPage + 1);
            else
            {

                hplForward.Visible = false;
            }

            rptAddress.DataSource = pds;
            rptAddress.DataBind();
            lblCurrent.Text = CurrentPage.ToString();
            lblTotal.Text = NumPage.ToString();
        }
    }
}